package org.lib.jpamysql.query.spel;

import java.util.List;

import org.lib.jpamysql.query.spel.dao.MyType1DAO;
import org.lib.jpamysql.query.spel.dao.MyType2DAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value="/myType")
@Slf4j
public class MyTypeController {

	@Autowired
	private MyType1DAO mt1Dao;
	
	@Autowired
	private MyType2DAO mt2Dao;
	
	// MyType1
	
	@PostMapping(value="/1/save")
	Long save(@RequestBody MyType1 t) {
		mt1Dao.save(t);
		log.info("MyType1 t.id={}", t.getId());
		return t.getId();
	}
	
	@GetMapping(value="/1/findAll")
	List<MyType1> findAllByAttribute1(String attribute) {
		return mt1Dao.findAllByAttribute(attribute);
	}
	
	// MyType2

	@PostMapping(value="/2/save")
	Long save(@RequestBody MyType2 t) {
		mt2Dao.save(t);
		log.info("MyType2 t.id={}", t.getId());
		return t.getId();
	}
	
	@GetMapping(value="/2/findAll")
	List<MyType2> findAllByAttribute2(String attribute) {
		return mt2Dao.findAllByAttribute(attribute);
	}
	
}
